// ----------------------------------------------------------------------------//
// Log4jConfigurator.java
// Created by user On 2012-10-12
package com.demo.framework.log;

import java.io.IOException;

import org.apache.log4j.Appender;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.varia.LevelRangeFilter;

import android.util.Log;

/**
 *
 */
public class LoggingConfiguration {
	public org.apache.log4j.Level mRootLevel = Level.ERROR;
	public org.apache.log4j.Level mAppLevel = Level.DEBUG;
	public String strAppPrefix = null;
	public String strLayoutPatten = "%d{yyyy-MM-dd HH:mm:ss,SSS} [%-5p][%c@%t] - %m%n";

	// public String strLogFile = null;

	public void setRootLevel(org.apache.log4j.Level rootLevel) {
		mRootLevel = rootLevel;
	}

	public void setAppPrefix(String appPrefix, org.apache.log4j.Level appLevel) {
		strAppPrefix = appPrefix;
		mAppLevel = appLevel;
	}

	public void setLayoutPatten(String layoutPatten) {
		strLayoutPatten = layoutPatten;
	}

	public void addFileLogger(String logfile) {
		try {
			Appender appender = new FileAppender(new PatternLayout(
					strLayoutPatten), logfile);
			org.apache.log4j.Logger root = org.apache.log4j.Logger
					.getRootLogger();
			root.addAppender(appender);
			Log.i("Log4jConfigurator", "Log file " + logfile + " created");
		} catch (IOException e) {
			Log.e("Log4jConfigurator", "Failed to create log file " + logfile,
					e);
		}
	}

	public void addAppender(Appender appender) {
		org.apache.log4j.Logger.getRootLogger().addAppender(appender);
	}

	public void addFileLogger(String logfile, org.apache.log4j.Level level) {
		try {
			Appender appender = new FileAppender(new PatternLayout(
					strLayoutPatten), logfile);
			LevelRangeFilter f = new LevelRangeFilter();
			f.setLevelMin(level);
			appender.addFilter(f);
			org.apache.log4j.Logger root = org.apache.log4j.Logger
					.getRootLogger();
			root.addAppender(appender);
			Log.i("Log4jConfigurator", "Log file " + logfile + " created");
		} catch (IOException e) {
			Log.e("Log4jConfigurator", "Failed to create log file " + logfile,
					e);
		}
	}

	// public void setLogFile(String logFile) {
	// strLogFile = logFile;
	// }

	public void configLog4j() {
		org.apache.log4j.Logger root = org.apache.log4j.Logger.getRootLogger();
		PatternLayout layout = new PatternLayout(strLayoutPatten);
		root.setLevel(mRootLevel);
		if (strAppPrefix != null)
			org.apache.log4j.Logger.getLogger(strAppPrefix).setLevel(mAppLevel);
		root.addAppender(new org.apache.log4j.ConsoleAppender(layout));

		// if (strLogFile != null) {
		// try {
		// Appender appender = new DailyRollingFileAppender(layout,
		// strLogFile, ".yyyy-MM-dd");
		// root.addAppender(appender);
		// Log.i("Log4jConfigurator", "Log file " + strLogFile
		// + " created");
		// } catch (IOException e) {
		// Log.e("Log4jConfigurator", "Failed to create log file "
		// + strLogFile, e);
		// }
		// }
	}
}

// ----------------------------------END---------------------------------------//